#!/bin/sh

# compares with lurette the results of:
#   lv6 -2c
#   lv6 -knc  -lv4 | lv6 -2c
#   lv6 -knpc -lv4 | lv6 -2c
#
# nb: thoses options are only useful only for lustre to lustre transformations
# (-2c and -exec disable them)

lustre_file=$1

shift
OPT=$@

node=`basename $lustre_file .lus`
_oracle=_"$node"_oracle.lus
oracle="$node"_oracle
lv4="$node"_lv4.lus
ec="$node".ec
lv4_node="$node__$node"
env=_"$node"_env.lut
env_node="$node"_env
tmpdir=/tmp/lustre-test-$node
v4_knc=$tmpdir/"$node"_knc
v4_knpc=$tmpdir/"$node"_knpc

. /home/jahier/.opam/opam-init/variables.sh
set -x verbose #echo on

# _$node_env.lut, $node.exec, etc have already been generated by compare_exec_and_2c

if
    [ -f $env_node ];
then
   echo "$env_node generated"

else if
     ./lus2lic $OPT $lustre_file -n $node -esa  --gen-autotest -np &&
     ./lus2lic $OPT $lustre_file -n $node -np -esa -lic -o $lustre_file.lic &&
     cat $lustre_file.lic  >> $_oracle;


then
     echo "./lus2lic $OPT $lustre_file -n $node --gen-autotest -np: ok"
else
    echo "Error"
    exit 2
fi
fi

# generate oracle exec
if
    ./lus2lic -dir $tmpdir $OPT $_oracle -n $oracle  -np -2c -cc -o $tmpdir/$oracle.exec;
then
    echo "lus2lic -dir $tmpdir $OPT $_oracle -n $oracle  -np -2c -cc -o $tmpdir/$oracle.exec: done"
else
    echo "lus2lic -dir $tmpdir $OPT $_oracle -n $oracle  -np -2c -cc -o $tmpdir/$oracle.exec: Error"
    exit 2
fi

# generate exec via -lv4 file using -knc
if
    ./lus2lic -dir $tmpdir $lustre_file -n $node -lv4 -knc -np $OPT  -o $v4_knc.lus;
then
    echo "lus2lic -dir $tmpdir $lustre_file -n $node -lv4 -knc -np $OPT -o $v4_knc.lus:  done"
else
    echo "lus2lic -dir $tmpdir $lustre_file -n $node -lv4 -knc -np $OPT -o $v4_knc.lus: error"
    exit 2
fi
if
    ./lus2lic -dir $tmpdir $OPT $v4_knc.lus -n $node  -2c -cc -o $v4_knc.exec;
then
    echo "lus2lic -dir $tmpdir $OPT $v4_knc.lus -n $node -2c -cc -o $v4_knc.exec: done"
else
    echo "lus2lic -dir $tmpdir $OPT $v4_knc.lus -n $node -2c -cc -o $v4_knc.exec: error"
    exit 2
fi

# generate exec via -lv4 file using -knpc
if
    ./lus2lic -dir $tmpdir $lustre_file -n $node -lv4 -knc -np $OPT -o $v4_knpc.lus;
then
    echo "lus2lic -dir $tmpdir $lustre_file -n $node -lv4 -knc -np $OPT -o $v4_knpc.lus: done"
else
    echo "lus2lic -dir $tmpdir $lustre_file -n $node -lv4 -knc -np $OPT -o $v4_knpc.lus: error"
    exit 2
fi
if
    ./lus2lic -dir $tmpdir $OPT $v4_knpc.lus -n $node  -2c -cc -o $v4_knpc.exec;
then
    echo "lus2lic -dir $tmpdir $OPT $v4_knpc.lus -n $node  -2c -cc -o $v4_knpc.exec: done"
else
    echo "lus2lic -dir $tmpdir $OPT $v4_knpc.lus -n $node  -2c -cc -o $v4_knpc.exec: error"
    exit 2
fi





PRECISION=4
if
    lurette -l 10   \
        -sut "$v4_knc.exec " \
        -env "lutin $env -n $env_node -p $PRECISION -seed 42" \
        -oracle "$tmpdir/$oracle.exec";
then
    echo "Comparison of 'lv6 -2c' and 'lv6 -lv4 -knc | lv6 -2c' with lurette on $lustre_file: ok"
    echo "lurette ok"
else
    echo "error"
     exit 2
fi
if
    lurette -l 10   \
        -sut "$v4_knpc.exec " \
        -env "lutin $env -n $env_node -p $PRECISION -seed 42" \
        -oracle "$tmpdir/$oracle.exec";
then
    echo "Comparison of  'lv6 -2c' and 'lv6 -lv4 -knpc | lv6 -2c' with lurette on $lustre_file: ok"
    echo "lurette ok"
else
    echo "error"
     exit 2
fi
exit 0
